package com.farmpost.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmpost.server.mapper.ReplyCommentMapper;
import com.farmpost.server.mapper.UserInfoMapper;
import com.farmpost.server.pojo.ReplyComment;
import com.farmpost.server.pojo.RespBean;
import com.farmpost.server.pojo.UserInfo;
import com.farmpost.server.service.IReplyCommentService;
import com.farmpost.server.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 回复表 服务实现类
 * </p>
 *
 * @author king
 * @since 2022-04-08
 */
@Service
public class ReplyCommentServiceImpl extends ServiceImpl<ReplyCommentMapper, ReplyComment> implements IReplyCommentService {

    @Autowired
    private ReplyCommentMapper replyCommentMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private IUserInfoService iUserInfoService;

    /**
     * 发表回复
     *
     * @param id        评论或回复本身id
     * @param toUid     评论或回复本身用户id
     * @param content
     * @param topicType
     * @return
     */
    @Override
    public RespBean postReply(Integer id, Integer toUid, String content, String topicType) {
        UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        ReplyComment replyComment = new ReplyComment();
        replyComment.setCommentId(id);//回复的父id
        replyComment.setReplyType(topicType);
        replyComment.setContent(content);
        replyComment.setFromUid(user.getUserId());
        replyComment.setToUid(toUid);
        replyComment.setReplyStatus(true);
        replyComment.setDateTime(LocalDateTime.now());
        replyComment.setReplyId((int) (Math.random() * 100000));//回复本身id
        int insert = replyCommentMapper.insert(replyComment);
        if (insert == 1) {
            return RespBean.success("回复成功");
        }
        return RespBean.error("插入回复失败");
    }

    /**
     * 根据id获取回复并据时间最新查询
     *
     * @param id
     * @param topicType
     * @param page
     * @return
     */
    @Override
    public List<ReplyComment> getReplyById(Integer id, String topicType, Page<ReplyComment> page) {
        QueryWrapper<ReplyComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("reply_status", true).orderByAsc("date_time");
        Page<ReplyComment> replyCommentPage;
        if (topicType.equals("comment")) {
            queryWrapper.eq("comment_id", id);
            replyCommentPage = replyCommentMapper.selectPage(page, queryWrapper);
        } else {
            queryWrapper.eq("reply_id", id);
            replyCommentPage = replyCommentMapper.selectPage(page, queryWrapper);
        }
        if (replyCommentPage == null || replyCommentPage.getTotal() == 0) {
            return new ArrayList<>();
        }
        List<ReplyComment> records = replyCommentPage.getRecords();
        for (int i = 0; i < records.size(); i++) {
            ReplyComment replyComment = records.get(i);
            UserInfo userInfo = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("user_id", replyComment.getFromUid()));
            userInfo.setUserPassword(null);
            replyComment.setUserInfo(userInfo);
            Integer replyCount = replyCommentMapper.selectCount(new QueryWrapper<ReplyComment>().eq("comment_id", replyComment.getReplyId()));
            replyComment.setCount(replyCount);
        }
        return records;
    }

    /**
     * 根据评论id查询回复
     *
     * @param topicId 评论id
     * @return
     */
    @Override
    public List<ReplyComment> queryReplyByTopicId(Integer topicId) {
        List<ReplyComment> replyComments = replyCommentMapper.selectList(new QueryWrapper<ReplyComment>().eq("comment_id", topicId).orderByAsc("date_time"));
        setReplyAttribute(replyComments);
        return replyComments;
    }

    private void setReplyAttribute(List<ReplyComment> replyComments) {
        for (ReplyComment replyComment : replyComments) {
            //获取当前回复的用户信息
            UserInfo userInfo = iUserInfoService.getUserInfoByUserId(replyComment.getFromUid());
            userInfo.setUserPassword(null);
            replyComment.setUserInfo(userInfo);
            //获取当前回复底下的回复数量
            Integer replyCount = replyCommentMapper.selectCount(new QueryWrapper<ReplyComment>().eq("reply_id", replyComment.getReplyId()));
            replyComment.setCount(replyCount);
        }
    }

    /**
     * 根据回复id查询回复
     *
     * @param id 回复id
     * @return
     */
    @Override
    public List<ReplyComment> queryReplyByReplyId(Integer id) {
        List<ReplyComment> replyLists = replyCommentMapper.selectList(new QueryWrapper<ReplyComment>().eq("reply_id", id).orderByAsc("date_time"));
        setReplyAttribute(replyLists);
        return replyLists;
    }
}
